var fs = require('fs')
const nodeXlsx = require('node-xlsx')
const path = require('path');
console.log(path.resolve("."));
const java = require('java');
// const Buffer = require('buffer')
let fileIndex = 0
require('core-js/modules/es.promise');
require('core-js/modules/es.string.includes');
require('core-js/modules/es.object.assign');
require('core-js/modules/es.object.keys');
require('core-js/modules/es.symbol');
require('core-js/modules/es.symbol.async-iterator');
require('regenerator-runtime/runtime');

const ExcelJS = require('exceljs/dist/es5')


let files = fs.readdirSync('./execlFile', function (err, files) {
    if (err) {
        console.log('Error', err);
    } else {
        console.log('Result', files);
    }
});
let dataArr = []
let options = {}
let nameArr = []
let count = 0
let buffer1 = null
let timer = null
let reStart = true
const writeFile = () => {
    console.log()
    options = {
        sheetOptions: {
            '!cols': dataArr[0].data[0].map(item => ({ wch: 15 })),
            '!rows': dataArr[0].data.map((item, index) => { if (index == 0) { return { hpx: 40 } } else { return { hpx: 100 } } })
        }
    }
    buffer1 = nodeXlsx.build(dataArr, options);
    fs.writeFileSync(path.join(__dirname, `./dist/${count}_${new Date().getTime()}.xls`), Buffer.from(buffer1));
    timer = setTimeout(() => {
        ++fileIndex
        reStart = true
        if (fileIndex < files.length) {
            count = 0
            nameArr = []
            dataArr = []
            options = {}
            buffer1 = null
            clearTimeout(timer)
            hebing()
        } else {
            console.log('success')
        }
    },3000)
}
const hebing = () => {
    if (fileIndex >= files.length) {
        writeFile()
        return
    }
    nameArr = files[fileIndex].split('_')
    count += +nameArr[3].replace('.xls','')
    console.log(fileIndex,files[fileIndex])
    if (reStart) {
        dataArr = nodeXlsx.parse(`./execlFile/${files[fileIndex]}`)
        ++fileIndex
        reStart = false
        hebing()
        return
    }
    if (count > 0 && count < 20000) {
        let data = nodeXlsx.parse(`./execlFile/${files[fileIndex]}`)
        data[0].data.splice(0, 1)
        dataArr[0].data = dataArr[0].data.concat(data[0].data)
        ++fileIndex
        hebing()
    } else if (count<28000&&count > 20000) {
        let data = nodeXlsx.parse(`./execlFile/${files[fileIndex]}`)
        data[0].data.splice(0, 1)
        dataArr[0].data = dataArr[0].data.concat(data[0].data)
        writeFile()
    } 
    else {
        nameArr = files[fileIndex].split('_')
        count -= (nameArr[3].replace('.xls',''))
        --fileIndex
        writeFile()
    }
}


hebing()
// files.forEach((it, index) => {
//     let data = []
//     console.log(index)
//     if (index === 0) {
//         dataArr = nodeXlsx.parse(`./execlFile/${it}`)
//     }
//     // 4153
//     if (index < 6000) {
//         let data = nodeXlsx.parse(`./execlFile/${it}`)
//         data[0].data.splice(0, 1)
//         dataArr[0].data = dataArr[0].data.concat(data[0].data)
//     }
// })


// const buffer = nodeXlsx.build(dataArr, options);
// fs.writeFileSync(path.join(__dirname, `./dist/data6001.xls`), Buffer.from(buffer));
